#ifndef BOOKITEMLIST_H
#define BOOKITEMLIST_H

#include <QtDeclarative/QDeclarativeItem>
#include <QDir>
#include <QUrl>

const int ITEM_BOOK_INDEX = Qt::UserRole+1;
const static QString PRIVATE_BOOKDIR= QDir::homePath()+"/.comicbook";
const static QString PRIVATE_BOOKDIR_EXTRACTED = PRIVATE_BOOKDIR+"/extracted";
const static QString PRIVATE_BOOKDIR_THUMBS = PRIVATE_BOOKDIR+"/thumbs";
const static QString ARCHIVE_DIRECTORY = QDir::homePath()+"/MyDocs/comicbooks";
const static QSize SCALED_ICON_SIZE = QSize(256,256);

class Book;
class BookUnzipper;
class BookUnrar;

class BookItemList : public QDeclarativeItem
{
    Q_OBJECT
    Q_DISABLE_COPY(BookItemList)
    Q_PROPERTY(QDeclarativeListProperty<Book> books READ books CONSTANT)        
    Q_ENUMS(ErrorCodes)

public:
    BookItemList(QDeclarativeItem *parent = 0);
    ~BookItemList();

    enum ErrorCodes{
        NoError,
        NotEnoughSpace,
        ZipBroken
    };

    enum CleanDirectories {
        Nothing,
        Thumbs,
        Extracted,
        CleanAll
    };

    QDeclarativeListProperty<Book> books();

    Q_INVOKABLE void openBook(int index);    
    Q_INVOKABLE void refreshBooks();

signals:
    void bookOpen(bool success, Book* book);
    void refreshBooksDone(bool foundBooks);
    void pluginReady();

private slots:
    void onExtractingFinished(BookItemList::ErrorCodes code);    
    void onFirstImageExtracted(BookItemList::ErrorCodes code, Book* book);
    void searchArchives();

private:    
    void cleanDirectories(CleanDirectories toClean);
    void createPrivateDirectories();

    static Book* atFunction(QDeclarativeListProperty<Book> *property, int index);
    static int countFunction(QDeclarativeListProperty<Book> *property);


private:
    QList<Book*>    m_books;
    BookUnzipper*   m_unzipper;
    BookUnrar*      m_unrar;
    int             m_openedBookIndex;
    int             m_bookCount;
};

QML_DECLARE_TYPE(BookItemList)
Q_DECLARE_METATYPE(BookItemList::ErrorCodes)

#endif // BOOKITEMLIST_H

